﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RobotBattle2.Utilities;
using System.CodeDom.Compiler;

namespace RobotBattle2.Scripting {
	public sealed class ConstantExpression : Expression {
		public ConstantExpression() { }
		public ConstantExpression(object value) {
			this.Value = value;
		}
		public ConstantExpression(SourceLocation location, object value) {
			this.Location = location;
			this.Value = value;
		}

		public SourceLocation Location { get; set; }
		public object Value { get; set; }

		public override void Dump(IndentedTextWriter writer) {
			switch (System.Type.GetTypeCode(Value.GetType())) {
				case TypeCode.String:
					writer.Write('"');
					writer.Write(((string)Value).Unescape());
					writer.Write('"');
					break;
				case TypeCode.Decimal:
					writer.Write(Value);
					break;
				case TypeCode.Boolean:
					writer.Write((bool)Value ? "_true" : "_false");
					break;
				default: throw new NotSupportedException();
			}
		}

		public override ExpressionType Type { get { return ExpressionType.Constant; } }
	}
}
